package cn.itsource.ymcc;

import java.util.concurrent.atomic.AtomicInteger;

public class CASTest2 {
        //使用AtomicInteger定义a
        static AtomicInteger a = new AtomicInteger();
        public static void main(String[] args) {
            CASTest2 test = new CASTest2();
            Thread[] threads = new Thread[5];
            for (int i = 0; i < 5; i++) {
                threads[i] = new Thread(() -> {
                    try {
                        for (int j = 0; j < 10; j++) {
                            //使用getAndIncrement函数进行自增操作
                            System.out.println(a.incrementAndGet());
                            Thread.sleep(500);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                threads[i].start();
            }
        }
}
